#include "chooselevel.h"
#include <QMenuBar>
#include <QPainter>
#include <QDebug>
#include "mypushbutton.h"
#include <QFile>
#include <QLabel>
#include <QSound>


ChooseLevel::ChooseLevel(QWidget *parent) : QMainWindow(parent)
{
//    /* 设置全局样式表 */
//    QFile file(":/style.qss");
//    if (file.exists()) {
//        file.open(QIODevice::ReadOnly);
//        QString styleSheet = file.readAll();
//        qApp->setStyleSheet(styleSheet);
//        file.close();
//    }
    /* 设置主窗口大小 */
    this->setFixedSize(QSize(320, 588));
    /* 设置窗口标题 */
    this->setWindowTitle("选择关卡");
    /* 设置窗口的图标 */
    this->setWindowIcon(QIcon(":/Image/Coin0001.png"));
    /* 获取菜单,创建菜单，菜单项, 并添加 */
    QMenuBar *mbar = menuBar();
    QMenu *startMenu = new QMenu("开始", this);
    QAction *backAction = new QAction("关闭", this);
    mbar->addMenu(startMenu);
    startMenu->addAction(backAction);
    /* 设置退出动作 */
    QSound * backsound = new QSound(":/Image/BackButtonSound.wav", this);
    connect(backAction, &QAction::triggered, [=](){
        this->close();

    });
    QSound * sound = new QSound(":/Image/TapButtonSound.wav", this);

    /* 创建返回的按钮 */
    //QPushButton *push_button = new QPushButton(this);
    MyPushButton *backButton = new MyPushButton(":/Image/BackButton.png", ":/Image/BackButtonSelected.png");
    backButton->setParent(this);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());
    /* 当返回按钮点击，发送自定义的信号给主窗口 */
    connect(backButton, &MyPushButton::clicked, this, [=](){
        emit backButtonClicked();
        backsound->play();
    });
    /* 创建选择关卡的按钮 */
    for (int i = 0; i < 20; i++) {
        MyPushButton *menuBtn = new MyPushButton(":/Image/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i % 4) * 70, 130 + (i / 4) * 70);
//        menuBtn->setText(QString::number(i + 1));
        /* 因为按钮是不规则的，所以文本的显示也不规则，用label来显示文本 */
        QLabel *label = new QLabel(QString::number(i + 1), this);
        /* 设置标签固定大小 */
        label->setFixedSize(menuBtn->width(), menuBtn->height());
        /* 设置标签和按钮位置匹配 */
        label->move(25 + (i % 4) * 70, 130 + (i / 4) * 70);
        /* 设置标签文字中心对齐 */
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet("QLabel { font:blod; color:#228B22 }");
        /* 设置鼠标事件穿透 */
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
        /* 监听点击事件 */
        connect(menuBtn, &MyPushButton::clicked, this, [=](){
            /* 音效 */
            sound->play();
            QString str = QString("你点击的是第%1关").arg(i + 1);
            qDebug() << str.toUtf8().data() ;

            curLevel = new PlayGame(i+1);
//            curLevel->setParent(this);
            /* 防止内存泄露 */
//            curLevel->setAttribute(Qt::WA_DeleteOnClose);
            this->hide();
            curLevel->show();
            /*  */
            connect(curLevel, &PlayGame::buttonBack, this, [=](){

                curLevel->hide();
                this->show();
                delete curLevel;
                curLevel = nullptr;

            });

        });

    }

}
/* 重写绘图事件 */
void ChooseLevel::paintEvent(QPaintEvent *)
{
    /* 加载背景图片 */
    QPixmap pix;
    bool ret = pix.load(":/Image/OtherSceneBg.png");
    if (!ret) {
        qDebug() << "load image failed";
        return;
    }
    /* 创建画家 */
    QPainter painter(this);
    /* 描绘背景 */
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
    /* 加载标题 */
    pix.load(":/Image/Title.png");
    /* 画标题 */
    painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix);
}

